<template>
  <div id="talentHistory">
    <!-- 有数据时 -->
    <div v-if="historyList.length != 0 && !loading">
      <div class="historyBox">
        <div class="historyItem" v-for="(item, index) in historyList" :key="'a' + index">
          <div class="historyItem_box">
            <div class="imgBox">
              <div class="imgBg" v-for="(item3, index3) in item.logo" :key="'d' + index3">
                <img v-if="item3[1] != 2" :src="item3[0]" v-errorImg:talent />

                <img v-if="item3[1] == 2 ||item3[1] == 3" src="@/assets/shi/detail.jpg" alt />
                <div v-if="item3[1] == 2" class="delete">已封号</div>
                <div v-if="item3[1]== 3" class="delete">已注销</div>
              </div>
            </div>
            <div class="compareBtn" v-if="item.status == 0">对比中，请稍等</div>
            <p class="compareNote" v-if="item.status == 0">正在努力对比中，请稍后查看</p>
            <div
              class="compareBtn"
              v-if="item.status == 2"
              @click="anew(item)"
              style="margin-bottom: 25px;cursor: pointer"
            >对比失败，请重新对比</div>

            <div
              class="compareBtn finishedBtn"
              v-if="item.status == 1"
              @click="toDetails(item.id)"
            >对比完成，查看详情</div>
            <p class="compareNote" v-if="item.status == 1">对比完成时间：{{ item.create_time }}</p>
          </div>
        </div>
      </div>
    </div>
    <div class="pageBox" v-if="historyList.length != 0 && totalCount > 12 && !loading">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="totalCount"
        :page-size="12"
        :current-page.sync="currentPage"
        @current-change="getListData"
        :hide-on-single-page="true"
      ></el-pagination>
    </div>
    <!-- 无数据时 -->
    <div v-if="!loading && historyList.length == 0" class="no_box">
      <div class="zw">
        <img src="@/assets/liu/zw.png" />
        <p>暂无数据~</p>
      </div>
    </div>
    <div
      v-loading="loading"
      v-if="loading"
      class="loading"
      element-loading-text="拼命加载中..."
      element-loading-spinner="el-icon-loading"
    ></div>
    <el-dialog :visible.sync="talentDetails" :show-close="true" v-if="talentDetails">
      <div class="topicDetailsBox">
        <talentDetails :inputName="item_id" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import talentDetails from './talentDetails'
export default {
  name: 'talentHistory',
  components: {},
  data() {
    return {
      activeName: 'first',
      // 分页
      historyList: [],
      currentPage: 1,
      totalCount: 0,
      loading: true,
      totalCount: 0,
      time: 1602309058000,
      cancel: null,
      talentDetails: false,
      item_id: '',
      timename: '',
      surplusCount: '',
    }
  },
  components: {
    talentDetails,
  },
  created() {
    // this.time = this.timestamp(this.time/1000, 'Y-M-D h:m:s')
    this.getListData()
    this.timename = setInterval(() => {
      this.getListData()
    }, 5000)
  },
  beforeDestroy() {
    clearTimeout(this.timename)
  },
  mounted() {},
  methods: {
    getListData() {
      let that = this
      that.loading = true
      this.$axios
        .get(
          '/api/author/comparerecord?api-version=2',
          {
            params: {
              page: that.currentPage,
              size: 12,
            },
          },
          {
            cancelToken: new that.$axios.CancelToken(function executor(c) {
              if (that.cancel != null) {
                that.cancel()
              }
              that.cancel = c
            }),
          }
        )
        .then((res) => {
          if (res.data.code == 0) {
            that.historyList = res.data.data
            that.totalCount = res.data.count
            let a = that.historyList.some((item) => {
              return item.status == 0
            })

            if (!a || that.historyList.length == 0) {
              clearTimeout(that.timename)
            }
          }
          that.loading = false
        })
        .catch((err) => {
          console.log(err)
        })
    },
    anew(item) {
      this.$axios
        .get('/api/customer/GetNumberofvisitsDetailed', {
          params: {
            API: '/api/author/addcompare',
          },
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.surplusCount = res.data.data.surplusCount
            if (res.data.data.surplusCount) {
              this.$axios
                .get('/api/author/addcompare?api-version=2', {
                  params: {
                    author_ids: item.author_id.toString(),
                    gid: item.id,
                  },
                })
                .then((res) => {
                  if (res.data.code == 0) {
                    this.getListData()
                  }
                })
                .catch((err) => {
                  console.log(err)
                })
            } else {
              this.$message({
                message: '您的对比次数已用完，请升级您的会员等级。',
                duration: 2000,
                customClass: 'includeMessage',
                offset: 154,
              })
            }
          }
        })
    },
    // 去详情
    toDetails(id) {
      this.item_id = id
      this.talentDetails = true
    },
  },
}
</script>

<style scoped lang="less">
#talentHistory {
  // padding-top: 17px;
  position: relative;
  border-radius: 4px;
  min-height: 84vh;
  background: #f1f1f1;
  .historyBox {
    width: 100%;
    display: flex;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;

    .historyItem {
      overflow: hidden;
      width: 25%;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;

      height: 192px;
      padding-right: 10px;
      border-radius: 4px;
      margin-bottom: 10px;

      .historyItem_box {
        background: #ffffff;
        padding-top: 30px;
        padding-bottom: 19px;
      }
      .imgBox {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        position: relative;

        .imgBg:not(:nth-of-type(1)) {
          margin-left: -12px;
        }

        .imgBg {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          position: relative;
          background-color: #fff;
          // margin-left: -12px;

          img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            border: 3px #ffffff solid;
          }
        }

        .delete {
          width: 51px;
          height: 20px;
          position: absolute;
          left: 45%;
          bottom: 0;
          transform: translate(-50%, 0);
          line-height: 24px;
          text-align: center;
          background-color: #f5f5f5;
          border-radius: 12px;
          border: solid 1px #e5e5e5;
          font-size: 14px;
          color: #222222;
          z-index: 3;
        }
      }

      .compareBtn {
        width: 190px;
        height: 40px;
        border-radius: 4px;

        font-size: 16px;
        line-height: 40px;
        text-align: center;
        margin: 0 auto;
        margin-top: 24px;
        border: solid 1px #e5e5e5;
        color: #888888;
      }

      .finishedBtn {
        color: #fd7f2c;
        border: solid 1px #fd7f2c;
        cursor: pointer;
      }
      .finishedBtn:hover {
        border-color: #ff924b;
        color: #ff924b;
      }
      .compareNote {
        font-size: 14px;
        color: #888888;
        text-align: center;
        padding-top: 14px;
      }
    }
  }
  .pageBox {
    box-sizing: border-box;

    width: 100%;
    overflow: hidden;
    position: absolute;
    top: 616px;
    left: 0;

    .pageText {
      float: left;
      height: 32px;
      line-height: 32px;
      font-size: 14px;
      color: #888888;
    }

    .el-pagination {
      float: right;
      // margin-right: 38px;
    }
  }
  .no_box {
    background: #ffffff;
    min-height: 85vh;
  }
  .zw {
    position: absolute;
    top: 288px;
  }
  .topicDetailsBox {
    background-color: #ffffff;
    border-radius: 4px;
    // overflow: auto;
    // height: 923px;
    // min-width: 804px;
    // min-width: 1200px;
  }
}
</style>
